package org.ww.algorithm.hash;

import java.util.Scanner;

public class HashTab_Google {
    public static void main(String[] args) {
        //创建哈希表
        HashTab hashTab = new HashTab(7);

        //写一个简单的菜单
        String key = "";
        Scanner scanner = new Scanner(System.in);
        while(true) {
            System.out.println("add:  添加雇员");
            System.out.println("list: 显示雇员");
            System.out.println("find: 查找雇员");
            System.out.println("exit: 退出系统");

            key = scanner.next();
            switch (key) {
                case "add":
                    System.out.println("输入id");
                    int id = scanner.nextInt();
                    System.out.println("输入名字");
                    String name = scanner.next();
                    //创建 雇员
                    Emp emp = new Emp(id, name);
                    hashTab.addEmp(emp);
                    break;
                case "list":
                    hashTab.list();
                    break;
                case "find":
                    System.out.println("请输入要查找的id");
                    id = scanner.nextInt();
                    hashTab.findById(id);
                    break;
                case "exit":
                    scanner.close();
                    System.exit(0);
                default:
                    break;
            }
        }
    }

}

class HashTab{
    private EmpLinkedList[] empLinkedLists;
    private int size;

    public HashTab(int size) {
        empLinkedLists=new EmpLinkedList[size];
        this.size=size;
        for(int i=0;i<size;i++){
            empLinkedLists[i]=new EmpLinkedList();
        }
    }

    //添加雇员节点信息
    public void addEmp(Emp emp){
        int i = hashFun(emp.id);
        empLinkedLists[i].addEmp(emp);
    }

    //遍历雇员节点信息
    public void list(){
        for(int i=0;i<size;i++){
            empLinkedLists[i].list();
        }
    }

    public void findById(int id){
        int i = hashFun(id);
        Emp emp = empLinkedLists[i].findById(id);
        if(emp==null){
            System.out.println("没有找到");
        }else{
            System.out.println("找到了:"+emp);
        }

    }

    public int hashFun(int id){
        return id%size;
    }
}

class EmpLinkedList{
    Emp header;

    //添加雇员
    public void addEmp(Emp emp){
        if(header==null){
            header=emp;
            return ;
        }
        //找到队尾进行添加
        Emp cur=header;
        while(true){
            if(cur.next==null){
                break;
            }
            cur=cur.next;
        }
        cur.next=emp;
    }

    //遍历雇员
    public void list(){
        if(header==null){
            System.out.println("当前雇员列表为空");
            return ;
        }
        Emp cur=header;
        while(true){
            System.out.printf("id=%d ---- name=%s",cur.id,cur.name);
            if(cur.next==null){
                break;
            }
            cur=cur.next;
        }
    }

    //查找雇员
    public Emp findById(int id){
        if(header==null){
            System.out.println("链表为空");
            return null;
        }
        Emp cur=header;
        while(true){
            if(cur.id==id){
                break;
            }
            if(cur.next==null){
                break;
            }
            cur=cur.next;
        }
        return cur;
    }
}

class Emp{
    int id;
    String name;
    Emp next;

    public Emp(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
